/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.list;

import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;
import java.text.DateFormat;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.border.Border;

import com.pearreview.util.review.Meeting;

public class MeetingListCellRenderer extends JPanel implements ListCellRenderer{
	private static final long serialVersionUID = 2309339681429802185L;
	
	public static final Color HIGHLIGHT_COLOR = Color.LIGHT_GRAY;
	
	private String dateCaptionText = "Date";
	private String locationCaptionText = "Location";
	private String protocolCaptionText = "Protocol";
	
	private String protocolNotAvailableCaption = "-";
	private String protocolAvailableCaption = "x";
	
	//private Font font = new Font("Tahoma", Font.PLAIN, 12);
	
	private Meeting selectedMeeting = null;
	
	private JLabel dateLabel;
	private JLabel locationLabel;
	private JLabel protocolLabel;
	
	private JLabel dateCaption;
	private JLabel locationCaption;
	private JLabel protocolCaption;
	
	private Border focusBorder;
	private Border noFocusBorder;
	
	public MeetingListCellRenderer() {
		initComponents();
	}
	
	public void setHighlightMeeting(Meeting selectedMeeting) {
		this.selectedMeeting = selectedMeeting;
	}
	
	public Meeting getHighlightMeeting() {
		return selectedMeeting;
	}
	
	@Override
	public void updateUI() {
		super.updateUI();
		focusBorder = UIManager.getBorder("List.focusCellHighlightBorder");
		Insets i = focusBorder.getBorderInsets(this);
		noFocusBorder = BorderFactory.createEmptyBorder(i.top,
				i.left, 
				i.bottom, 
				i.right);
	}
	
	private void initComponents() {
		setFocusable(true);
		
		dateLabel = new JLabel();
		locationLabel = new JLabel();
		protocolLabel = new JLabel();
		
		dateCaption = new JLabel();
		locationCaption = new JLabel();
		protocolCaption = new JLabel();
		
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup()
						.addComponent(dateCaption)
						.addComponent(locationCaption)
						.addComponent(protocolCaption))
					.addGap(30) // gaps between two columns
					.addGroup(layout.createParallelGroup()
						.addComponent(protocolLabel)
						.addComponent(locationLabel)
						.addComponent(dateLabel)))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup()
							.addComponent(dateCaption)
							.addComponent(dateLabel))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(layout.createParallelGroup()
							.addComponent(locationCaption)
							.addComponent(locationLabel))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(layout.createParallelGroup()
							.addComponent(protocolCaption)
							.addComponent(protocolLabel))
					.addContainerGap())
		);
	}
	
	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		
		if(!(value instanceof Meeting)) {
			DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
			dlcr.getListCellRendererComponent(list, 
					value, 
					index, 
					isSelected, 
					cellHasFocus);
			
			return dlcr;
		}
		
		Meeting meeting = (Meeting) value;
		String date = DateFormat.getDateInstance().format(
				meeting.getPlannedDate().getTime());
		String location = meeting.getPlannedLocation();
		String protocol = getProtocolNotAvailableCaption();
		if(meeting.getProtocol() != null) {
			protocol = getProtocolAvailableCaption();
			Calendar cal = meeting.getProtocol().getDate();
			if(cal != null)
				protocol = DateFormat.getDateInstance().format(cal.getTime());
				
		}
		
		if(cellHasFocus) {
			setBorder(focusBorder);
		} else {
			setBorder(noFocusBorder);
		}
		
		Color color = isSelected ? 
				list.getSelectionForeground() : list.getForeground();
		dateCaption.setForeground(color);
		locationCaption.setForeground(color);
		protocolCaption.setForeground(color);
		dateLabel.setForeground(color);
		locationLabel.setForeground(color);
		protocolLabel.setForeground(color);
		
		if(isSelected) {
			setBackground(list.getSelectionBackground());
		} else if(meeting.equals(selectedMeeting)) {
			setBackground(HIGHLIGHT_COLOR);
		} else {
			setBackground(list.getBackground());
		}
		
		dateCaption.setText(getDateCaption());
		dateLabel.setText(date);
		
		locationCaption.setText(getLocationCaption());
		locationLabel.setText(location);
		
		protocolCaption.setText(getProtocolCaption());
		protocolLabel.setText(protocol);
		
		setFont(list.getFont());
		
		dateCaption.setFont(getFont());
		locationCaption.setFont(getFont());

		protocolCaption.setFont(getFont());
		dateLabel.setFont(getFont());
		
		locationLabel.setFont(getFont());
		protocolLabel.setFont(getFont());
		
		return this;
	}

	public void setDateCaption(String dateCaption) {
		this.dateCaptionText = dateCaption;
	}

	public String getDateCaption() {
		return dateCaptionText;
	}

	public void setLocationCaption(String locationCaption) {
		this.locationCaptionText = locationCaption;
	}

	public String getLocationCaption() {
		return locationCaptionText;
	}

	public void setProtocolCaption(String protocolCaption) {
		this.protocolCaptionText = protocolCaption;
	}

	public String getProtocolCaption() {
		return protocolCaptionText;
	}

	public void setProtocolNotAvailableCaption(
			String protocolNotAvailableCaption) {
		this.protocolNotAvailableCaption = protocolNotAvailableCaption;
	}

	public String getProtocolNotAvailableCaption() {
		return protocolNotAvailableCaption;
	}

	public void setProtocolAvailableCaption(String protocolAvailableCaption) {
		this.protocolAvailableCaption = protocolAvailableCaption;
	}

	public String getProtocolAvailableCaption() {
		return protocolAvailableCaption;
	}
}
